// miniprogram/pages/test/detail/detail.js
const cloud_testAnswer = wx.cloud.database().collection('testAnswer')
var time_utils = require('../../../utils/time_utils');

Page({
  // testAnswer由\classInfo\testData\testStatus\publishTimes\组成
  // testAnswer由\classInfo\testData\testStatus\publishTimes\组成
  // testAnswer由\classInfo\testData\testStatus\publishTimes\组成
  data: {
    testData: {},
    curIndex: 0,
    unFinish: 0,
    classInfo: {},
    publishTimes: [],
    showTestCard: false,
    publishId: '' //答案id
  },
  onLoad: function (options) {
    console.log("options", options)
    var publishId = options.publishId
    if (publishId != null) {
      console.log(publishId)
      cloud_testAnswer.doc(publishId).get().then(res => {
        console.log(res)
        var answerData = res.data
        this.setData({
          publishId: publishId,
          testData: answerData.testData,
          classInfo: answerData.classInfo,
          publishTimes: answerData.publishTimes
        })
      })
    } else {
      var classInfo = JSON.parse(decodeURIComponent(options.classInfo))
      var testItem = JSON.parse(decodeURIComponent(options.testItem))
      this.setData({
        testData: testItem,
        classInfo: classInfo
      })
    }
  },
  onShareAppMessage: function () {

  },
  inputListener(e) {
    var data = e.detail.value
    console.log(data)
    var curIndex = this.data.curIndex
    var testData = this.data.testData
    testData.TestList[curIndex].answer = data
    this.setData({
      testData: testData
    })
  },
  //上一题点击监听
  onPreTestClick(e) {
    console.log(e)
    var curIndex = this.data.curIndex
    this.setData({
      curIndex: curIndex - 1
    })
  },
  //下一题点击监听
  onNextTestClick(e) {
    console.log(e)
    var curIndex = this.data.curIndex
    this.setData({
      curIndex: curIndex + 1
    })
  },
  //显示答题卡
  showTestCard(e) {
    var testList = this.data.testData.TestList
    // console.log("TestList", testList)
    var unFinish = 0
    for (let i = 0; i < testList.length; i++) {
      // console.log("test", testList[i])
      if (!testList[i].hasOwnProperty('answer')) {
        unFinish++;
      }
    }
    console.log("unFinish", unFinish)
    this.setData({
      unFinish: unFinish,
      showTestCard: true
    })
  },
  //答题卡里面的按钮点击监听
  onBtnClick(e) {
    console.log(e)
    var flag = e.detail
    if (flag == 'publishData') {
      wx.showModal({
        title: '提示',
        content: '交卷后答案会保存，确定交卷吗?',
        cancelText: '继续答题',
        showCancel: true,
        success: res => {
          if (res.confirm) {
            console.log('用户点击确定')
            this.submitTestProcess()
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    this.setData({
      showTestCard: false
    })
  },
  //答题卡里面的item点击监听
  onTestItemClick(e) {
    console.log("onTestItemClick", e)
    var index = e.detail
    this.setData({
      curIndex: index,
      showTestCard: false
    })
  },
  //提交答案逻辑
  submitTestProcess(e) {
    var publishId = this.data.publishId
    if (publishId != null && publishId.length > 0) {
      this.updateTestAnswer(publishId)
    } else {
      this.submitTest()
    }
  },
  //提交试卷答案
  submitTest(e) {
    var dateTime = time_utils.getDateTime()
    console.log("dateTime", dateTime)
    //提交答案
    var answerObj = {
      testStatus: '等待管理员评分',
      testData: this.data.testData,
      classInfo: this.data.classInfo,
      testName: this.data.testData.TestName,
      publishTimes: [dateTime]
    }
    cloud_testAnswer.add({
        data: answerObj
      }).then(res => {
        console.log("提交答案成功", res)
        this.publishSuccess(2)
      })
      .catch(console.error)
  },
  //更新自己的答案
  updateTestAnswer(publishId) {
    var testData = this.data.testData
    var publishTimes = this.data.publishTimes
    // 添加当前答题时间
    var dateTime = time_utils.getDateTime()
    publishTimes.push(dateTime)
    cloud_testAnswer.doc(publishId).update({
        data: {
          testData: testData,
          testStatus: "员工重新答题",
          publishTimes: publishTimes
        },
      }).then(res => {
        console.log("判分成功")
        this.publishSuccess(1)
      })
      .catch(console.error)
  },
  //提交答案成功
  publishSuccess(num) {
    wx.showToast({
      title: '提交成功',
      icon: 'none'
    });
    setTimeout(function () {
      //界面回退
      wx.navigateBack({
        delta: num,
        success: function () {
          console.log("navigateBack-success")
        }
      })
    }, 1500);
  }
})